<?php


/** ensure this file is being included by a parent file */
defined('_VALID_MOS') or die('Direct Access to this location is not allowed.');

class HelpDeskAddon extends HelpDeskObject 
{
	/**
	 * 
	 * @param $row
	 * @return unknown_type
	 */
	function getContents( $row, &$msg )
	{
		$type = $row->type;
		$filename = $row->filename;
			
		$getDirectory = &HelpDeskAddon::getDirectory( $type );
		if (!isset($getDirectory['baseDir']) ) 
		{
			$message = HelpDeskText::_('No File Type Selected');
			$msg->setError( $message );
			return false; 
		}
		
		$baseDir = $getDirectory['baseDir'];
		$dirname = $getDirectory['dirname'];
	
		// if file not writable, fail and give notice
		if ( !is_writable( $baseDir . $filename . ".php" ) ) 
		{
			$message = HelpDeskText::_('File Not Writeable').": ". $baseDir . $filename . ".php";
			$msg->setError( $message );
			return false; 	
		}
	
		// if file exists, grab contents and send for editing
		if (file_exists( $baseDir . $filename . ".php" )) 
		{ 
			// get the contents of the file 
			$contents = file_get_contents( $baseDir . $filename . ".php" );
			return $contents;
		} else 
		{
			$message = HelpDeskText::_('File Not Present').": ". $baseDir . $filename . ".php";
			$msg->setError( $message );
			return false; 
		}
	}
	/**
	 * Returns a directory
	 * @param mixed Boolean
	 * @param mixed Boolean
	 * @return array
	 */
	function &getDirectory( $type ) 
	{
		$return = "";
		
		switch ($type) {
			case "lang":
			default:
				$return = array();
				$return['baseDir'] = mosPathName( JPATH_COMPONENT_SITE.DS.'languages' );
				$return['dirname'] = 'languages';
			  break;
		} // end switch type

		return $return;
	}	

	/**
	* getAddonAttribute
	*/
	function getAddonAttribute( $filename, $type, $attribute ) 
	{
		global $mosConfig_absolute_path;
		
		$getDirectory = &HelpDeskAddon::getDirectory( $type );
		if (!isset($getDirectory['baseDir']) ) {
				echo HelpDeskText::_('No File Type Selected')."<br />";
				return false;			
		}
		
		$baseDir = $getDirectory['baseDir'];
		$dirname = $getDirectory['dirname'];
	
		// if file not writable, fail and give notice
		if ( !is_writable( $baseDir . $filename . ".php" ) ) {
			echo $baseDir . $filename . ".php<br />";
			echo HelpDeskText::_('Not Writeable File')."<br />";
			return false; 	
		}

		// if file exists, grab contents and send for editing
		if (file_exists( $baseDir . $filename . ".php" )) { 
			// get the contents of the file 
			$contents = file_get_contents( $baseDir . $filename . ".php" );
		} else {
			echo $baseDir . $filename . ".php<br />";
			echo HelpDeskText::_('No File')."<br />";
			return false; 
		}

		$return = "";
		// define the regular expression
		$regex = "#<".$attribute."(.*?)>(.*?)</".$attribute.">#s";
		
		//Find all instances of plugin and put in $matches
		preg_match_all( $regex, $contents, $matches );
		
		//Number of instances
		$count = count( $matches[0] );
					 
		//if instances 
		if ( $count ) {
			//Get parameters
			unset($thisposition);

			$thisposition[0] = $matches[0][0]; // the full text of the matches {attribute params}contents{/attribute}
			$thisposition[1] = $matches[1][0]; // the params {attribute THIS}contents{/attribute}
			$thisposition[2] = $matches[2][0]; // the contents {attribute}THIS{/attribute}
			$return = $matches[2][0]; // the contents {attribute}THIS{/attribute}
		}
		
		return $return;
	} // end getAddonAttribute

	/**
	* install
	*/
	function installAddon( $filename, $type ) 
	{
		global $mosConfig_absolute_path, $database;

		if (!$type) {
			$this->setError(HelpDeskText::_('No File Type Selected'));
			return false;
		}	
		
		// Insert into DB
		$query = "SELECT `id` FROM #__helpdesk_addons "
		. "\n WHERE `type` = " . $database->Quote( $type )
		. " AND `filename` = " . $database->Quote( $filename )
		;
		$database->setQuery( $query );
		if (!$database->query()) {
			$this->throwError( HelpDeskText::_( 'SQL Error' ) . ": " . $database->stderr( true ) );
			return false;
		}

		$id = $database->loadResult();
		if ($id) {
			$this->throwError( HelpDeskText::_( 'Addon' ).' "' . $filename . '"'.HelpDeskText::_( 'Already Exists' ).'!' );
			return false;
		}
		
		if (!$id) {
			//	  <name>Add-on Name</name>
			//    <author>Dioscouri.com</author>
			//    <authorEmail>info@dioscouri.com</authorEmail>
			//    <authorUrl>http://www.dioscouri.com</authorUrl>
			//    <version>0.1</version>
			//    <creationDate>2008-02-01</creationDate>
			//    <description>This Add-on does this function.</description>
		
			// $row = new HelpDeskAddons( $database );
			$row = HelpDeskTable::getInstance( 'Addon', 'Table' );
			$row->type			= $type;
			$row->filename		= $filename;
			$row->iscore 		= 0;
			$row->title 		= $this->getAddonAttribute( $filename, $type, "name" );
			$row->description	= $this->getAddonAttribute( $filename, $type, "description" );
			$row->version 		= $this->getAddonAttribute( $filename, $type, "version" );
			$row->created_datetime 	= $this->getAddonAttribute( $filename, $type, "creationDate" );
			$row->author_name 		= $this->getAddonAttribute( $filename, $type, "author" );
			$row->author_email 		= $this->getAddonAttribute( $filename, $type, "authorEmail" );
			$row->author_url 		= $this->getAddonAttribute( $filename, $type, "authorUrl" );
			$row->store();
			return true;
		} 
	} // end installAddon


	/**
	* uploadAddonFile
	*/
	function uploadAddonFile( $filename, $userfile_name, $type, &$msg ) 
	{
		global $mosConfig_absolute_path;

		$getDirectory = &HelpDeskAddon::getDirectory( $type );
		if (!isset($getDirectory['baseDir']) ) {
				echo HelpDeskText::_('No File Type Selected')."<br />";
				return false;			
		}
		
		$baseDir = $getDirectory['baseDir'];
		$dirname = $getDirectory['dirname'];

		if (file_exists( $baseDir . $userfile_name )) { $this->setError( 'Upload failed as file already exists.' ); return false; }
	
		if (file_exists( $baseDir )) {
			if (is_writable( $baseDir )) {
				if (move_uploaded_file( $filename, $baseDir . $userfile_name )) {
					if (mosChmod( $baseDir . $userfile_name )) {
						return true;
					} else {
						$this->setError( HelpDeskText::_('Failed to change the permissions of the uploaded file.') );
					}
				} else {
					$this->setError( HelpDeskText::_('Failed to move uploaded file to <code>/'.$dirname.'</code> directory.') );
				}
			} else {
				$this->setError( HelpDeskText::_('Upload failed as <code>/'.$dirname.'</code> directory is not writable.') );
			}
		} else {
			$this->setError( HelpDeskText::_('Upload failed as <code>/'.$dirname.'</code> directory does not exist.') );
		}
		return false;
	} // end uploadAddonFile

} // end class 